import java.util.Arrays;
import java.util.HashSet;

/**
 * @author ZhiWen Ren
 * @version 1.0
 * @description: LeetCode-322-零钱兑换
 * @date 2025/8/11 14:46
 */
public class Solution322 {
    public int coinChange(int[] coins, int amount) {
        // 解决这道题目关键在于理解决策树(LeetCode题解已给出)
        // dp[i]表示构成总额为i的零钱的最小数量
        int max = amount + 1;
        int[] dp = new int[amount + 1];
        Arrays.fill(dp, max);
        dp[0] = 0;

        for (int i = 1; i <= amount; i++) {
            for (int j = 0; j < coins.length; j++) {
                if (coins[j] <= i) {
                    // 确保只有硬币数值小于amount时，才计算dp
                    dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1);
                }
            }
        }

        return dp[amount] > amount ? -1 : dp[amount];
    }
}
